var jweixin = require('../../static/js/jweixin-module-master/src/index.js');

export default {
	//判断是否在微信中  
	isWechat: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	},
	//在需要自定义分享的页面中调用  
	share: function(urls, types, token, b, shopinfo, cid) {
		if (!this.isWechat()) {
			return;
		}
		uni.request({
			url: 'http://rticle.api.yimiguoji.com/shop.php/WxInfo/getConfig',
			method: "GET",
			data: {
				url: urls,
				type: types,
				cid: cid
			},
			header: {
				'token': token,
				"b": b,
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			success: (res) => {
				jweixin.config({
					debug: false,
					appId: res.data.data.config.appId,
					timestamp: res.data.data.config.timestamp,
					nonceStr: res.data.data.config.nonceStr,
					signature: res.data.data.config.signature,
					jsApiList: [
						'updateAppMessageShareData',
						'updateTimelineShareData',
						"onMenuShareAppMessage"
					]
				});
				jweixin.ready(function(rest) {
					var f = '易米国际-全球精品海购'
					if (types == "goods") {
						if (shopinfo.goodsinfo.subtitle) {
							f = shopinfo.goodsinfo.subtitle
						}
						jweixin.updateAppMessageShareData({
							title: shopinfo.goodsinfo.title,
							desc: f,
							link: res.data.data.shareinfo.url,
							imgUrl: shopinfo.goodsinfo.thumb_url[0],
							success: function(res) {
								
							}
						})
						jweixin.updateTimelineShareData({
							title: shopinfo.goodsinfo.title,
							link: res.data.data.shareinfo.url,
							imgUrl: shopinfo.goodsinfo.thumb_url[0],
							success: function(res) {
								//用户点击分享后的回调
							}
						})
						
						jweixin.onMenuShareAppMessage({
						  title: shopinfo.goodsinfo.title, // 分享标题
						  desc: f, // 分享描述
						  link:res.data.data.shareinfo.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
						  imgUrl: shopinfo.goodsinfo.thumb_url[0], // 分享图标
						  type: '', // 分享类型,music、video或link，不填默认为link
						  dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
						  success: function () {
						    // 用户点击了分享后执行的回调函数
						  }
						})
					} else {
						jweixin.updateAppMessageShareData({
							title: res.data.data.shareinfo.title,
							desc: f,
							link: res.data.data.shareinfo.url,
							imgUrl: res.data.data.shareinfo.imgurl,
							success: function(res) {
								//用户点击分享后的回调
								
							}
						})
						jweixin.updateTimelineShareData({
							title: res.data.data.shareinfo.title,
							link: res.data.data.shareinfo.url,
							imgUrl: res.data.data.shareinfo.imgurl,
							success: function(res) {
								//用户点击分享后的回调
							}
						})
						
						jweixin.onMenuShareAppMessage({
						  title: res.data.data.shareinfo.title, // 分享标题
						  desc: f, // 分享描述
						  link: res.data.data.shareinfo.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
						  imgUrl: res.data.data.shareinfo.imgurl, // 分享图标
						  type: '', // 分享类型,music、video或link，不填默认为link
						  dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
						  success: function () {
						    // 用户点击了分享后执行的回调函数
						  }
						})
					}
				});
			}
		});
	}
}
